package cn.yzking.studentmanagement;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import cn.yzking.studentmanagement.databinding.ActivityManagementBinding;
import cn.yzking.studentmanagement.fragment.IndexFragment;
import cn.yzking.studentmanagement.fragment.MineFragment;

public class ManagementActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManagementBinding binding = ActivityManagementBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 加载fragment
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().setReorderingAllowed(true).add(R.id.fragment_container_view, IndexFragment.class, null).commit();
        }
        // 加载addStudentMenuItem
        MenuItem addStudentMenuItem = binding.appBar.getMenu().getItem(0);
        addStudentMenuItem.setOnMenuItemClickListener(item1 -> {
            Intent intent = new Intent(this, AddStudentActivity.class);
            startActivity(intent);
            return true;
        });

        binding.bottomNavigation.setOnItemSelectedListener(item -> {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setReorderingAllowed(true);

            int itemId = item.getItemId();
            if (itemId == R.id.index) {
                addStudentMenuItem.setVisible(true);

                fragmentTransaction.replace(R.id.fragment_container_view, IndexFragment.class, null);
                fragmentTransaction.commit();
                return true;
            } else if (itemId == R.id.mine) {
                addStudentMenuItem.setVisible(false);

                Bundle bundle = new Bundle();
                bundle.putString("username", getIntent().getStringExtra("username"));

                fragmentTransaction.replace(R.id.fragment_container_view, MineFragment.class, bundle);
                fragmentTransaction.commit();
                return true;
            }
            return false;
        });
    }
}